<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Contact</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.12.0/dist/cdn.min.js" defer></script>
    <style>
        body {
            background-color: #f4f7fc;
            font-family: 'Arial', sans-serif;
        }
        .container {
            max-width: 600px;
            margin-top: 50px;
        }
        .card {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group label {
            font-weight: bold;
        }
        .form-control {
            border-radius: 5px;
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
            font-size: 16px;
        }
        .btn-primary {
            background-color: #007bff;
            border-color: #007bff;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 5px;
        }
        .btn-primary:hover {
            background-color: #0056b3;
            border-color: #004085;
        }
        .alert {
            display: none;
        }
    </style>
</head>
<body>
<div class="container" x-data="contactForm()">
    <div class="card">
        <h1>Add New Contact</h1>

        <!-- Add Contact Form -->
        <form @submit.prevent="submitForm">
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" id="name" class="form-control" x-model="formData.name" required />
            </div>

            <div class="form-group">
                <label for="phoneNumber">Phone Number</label>
                <input type="text" id="phoneNumber" class="form-control" x-model="formData.phoneNumber" required />
            </div>

            <div class="form-group">
                <label for="email">Email</label>
                <input type="email" id="email" class="form-control" x-model="formData.email" required />
            </div>

            <div class="form-group">
                <label for="school">School</label>
                <input type="text" id="school" class="form-control" x-model="formData.school" />
            </div>

            <div class="form-group">
                <label for="gender">Gender</label>
                <select id="gender" class="form-control" x-model="formData.gender">
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                </select>
            </div>

            <div class="form-group">
                <label for="birthYear">Birth Year</label>
                <input type="number" id="birthYear" class="form-control" x-model="formData.birthYear" />
            </div>

            <div class="form-group">
                <label for="major">Major</label>
                <input type="text" id="major" class="form-control" x-model="formData.major" />
            </div>

            <button type="submit" class="btn btn-primary btn-block">Save</button>
            <div class="alert alert-success mt-3" x-show="message" x-text="message"></div>
        </form>
    </div>
</div>

<script>
    function contactForm() {
        return {
            formData: {
                name: '',
                phoneNumber: '',
                email: '',
                school: '',
                gender: 'Male',
                birthYear: '',
                major: ''
            },
            message: '',
            async submitForm() {
                const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
                const phoneRegex = /^1[3-9]\d{9}$/;

                if (!emailRegex.test(this.formData.email)) {
                    alert("Please enter a valid email address.");
                    return;
                }
                if (!phoneRegex.test(this.formData.phoneNumber)) {
                    alert("Please enter a valid phone number.");
                    return;
                }

                try {
                    const response = await fetch('/contacts/add', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(this.formData),
                    });

                    if (response.ok) {
                        this.message = 'Contact saved successfully!';
                        this.resetForm();
                        window.history.back();  // 返回上一页面
                    } else {
                        this.message = 'Failed to save contact. Please try again.';
                    }
                } catch (error) {
                    console.error('Error:', error);
                    this.message = 'An error occurred. Please try again.';
                }
            },
            resetForm() {
                this.formData = {
                    name: '',
                    phoneNumber: '',
                    email: '',
                    school: '',
                    gender: 'Male',
                    birthYear: '',
                    major: ''
                };
            }
        };
    }
</script>
</body>
</html>

<!--
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Contact</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.12.0/dist/cdn.min.js" defer></script>
    <style>
        body {
            background-color: #f4f7fc;
            font-family: 'Arial', sans-serif;
        }
        .container {
            max-width: 600px;
            margin-top: 50px;
        }
        .card {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group label {
            font-weight: bold;
        }
        .form-control {
            border-radius: 5px;
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
            font-size: 16px;
        }
        .btn-primary {
            background-color: #007bff;
            border-color: #007bff;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 5px;
        }
        .btn-primary:hover {
            background-color: #0056b3;
            border-color: #004085;
        }
        .alert {
            display: none;
        }
    </style>
</head>
<body>
<div class="container" x-data="contactForm()">
    <div class="card">
        <h1>Add New Contact</h1>

        &lt;!&ndash; Add Contact Form &ndash;&gt;
        <form @submit.prevent="submitForm">
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" id="name" class="form-control" x-model="formData.name" required />
            </div>

            <div class="form-group">
                <label for="phoneNumber">Phone Number</label>
                <input type="text" id="phoneNumber" class="form-control" x-model="formData.phoneNumber" required />
            </div>

            <div class="form-group">
                <label for="email">Email</label>
                <input type="email" id="email" class="form-control" x-model="formData.email" required />
            </div>

            <div class="form-group">
                <label for="school">School</label>
                <input type="text" id="school" class="form-control" x-model="formData.school" />
            </div>

            <div class="form-group">
                <label for="gender">Gender</label>
                <select id="gender" class="form-control" x-model="formData.gender">
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                </select>
            </div>

            <div class="form-group">
                <label for="birthYear">Birth Year</label>
                <input type="number" id="birthYear" class="form-control" x-model="formData.birthYear" />
            </div>

            <div class="form-group">
                <label for="major">Major</label>
                <input type="text" id="major" class="form-control" x-model="formData.major" />
            </div>

            <button type="submit" class="btn btn-primary btn-block">Save</button>
            <div class="alert alert-success mt-3" x-show="message" x-text="message"></div>
        </form>
    </div>
</div>

<script>
    function contactForm() {
        return {
            formData: {
                name: '',
                phoneNumber: '',
                email: '',
                school: '',
                gender: 'Male',
                birthYear: '',
                major: ''
            },
            message: '',
            async submitForm() {
                const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
                const phoneRegex = /^1[3-9]\d{9}$/;

                if (!emailRegex.test(this.formData.email)) {
                    alert("Please enter a valid email address.");
                    return;
                }
                if (!phoneRegex.test(this.formData.phoneNumber)) {
                    alert("Please enter a valid phone number.");
                    return;
                }

                try {
                    const response = await fetch('/contacts/add', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(this.formData),
                    });

                    if (response.ok) {
                        this.message = 'Contact saved successfully!';
                        this.resetForm();
                    } else {
                        this.message = 'Failed to save contact. Please try again.';
                    }
                } catch (error) {
                    console.error('Error:', error);
                    this.message = 'An error occurred. Please try again.';
                }
            },
            resetForm() {
                this.formData = {
                    name: '',
                    phoneNumber: '',
                    email: '',
                    school: '',
                    gender: 'Male',
                    birthYear: '',
                    major: ''
                };
            }
        };
    }
</script>
</body>
</html>
-->

<!--
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Contact</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f4f7fc;
            font-family: 'Arial', sans-serif;
        }
        .container {
            max-width: 600px;
            margin-top: 50px;
        }
        .card {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group label {
            font-weight: bold;
        }
        .form-control {
            border-radius: 5px;
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
            font-size: 16px;
        }
        .btn-primary {
            background-color: #007bff;
            border-color: #007bff;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 5px;
        }
        .btn-primary:hover {
            background-color: #0056b3;
            border-color: #004085;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="card">
        <h1>Add New Contact</h1>

        &lt;!&ndash; Add Contact Form &ndash;&gt;
        <form id="contactForm" th:action="@{/contacts/add}" th:object="${contact}" method="post">
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" id="name" class="form-control" th:field="*{name}" required />
            </div>

            <div class="form-group">
                <label for="phoneNumber">Phone Number</label>
                <input type="text" id="phoneNumber" class="form-control" th:field="*{phoneNumber}" required />
            </div>

            <div class="form-group">
                <label for="email">Email</label>
                <input type="email" id="email" class="form-control" th:field="*{email}" required />
            </div>

            <div class="form-group">
                <label for="school">School</label>
                <input type="text" id="school" class="form-control" th:field="*{school}" />
            </div>

            <div class="form-group">
                <label for="gender">Gender</label>
                <select id="gender" class="form-control" th:field="*{gender}">
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                </select>
            </div>

            <div class="form-group">
                <label for="birthYear">Birth Year</label>
                <input type="number" id="birthYear" class="form-control" th:field="*{birthYear}" />
            </div>

            <div class="form-group">
                <label for="major">Major</label>
                <input type="text" id="major" class="form-control" th:field="*{major}" />
            </div>

            <button type="submit" class="btn btn-primary btn-block">Save</button>
        </form>
    </div>
</div>

&lt;!&ndash; JS imports &ndash;&gt;
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>

<script>
    document.getElementById("contactForm").addEventListener("submit", function(event) {
        // 获取表单元素
        var email = document.getElementById("email").value;
        var phoneNumber = document.getElementById("phoneNumber").value;

        // 邮箱正则表达式：基本邮箱格式
        var emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
        // 中国大陆手机号正则表达式：以1开头，后面跟10位数字
        var phoneRegex = /^1[3-9]\d{9}$/;

        // 验证邮箱
        if (!emailRegex.test(email)) {
            alert("Please enter a valid email address.");
            event.preventDefault(); // 阻止表单提交
            return;
        }

        // 验证手机号
        if (!phoneRegex.test(phoneNumber)) {
            alert("Please enter a valid phone number.");
            event.preventDefault(); // 阻止表单提交
            return;
        }
    });
</script>

</body>
</html>
-->
